import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.CreateTagsRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.InstanceState;
import com.amazonaws.services.ec2.model.Placement;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.RunInstancesResult;
import com.amazonaws.services.ec2.model.StartInstancesRequest;
import com.amazonaws.services.ec2.model.StopInstancesRequest;
import com.amazonaws.services.ec2.model.Tag;
import com.amazonaws.services.ec2.model.TerminateInstancesRequest;


public class MyInstance {

	private AmazonEC2 ec2;
	private AWSCredentials cre;
	private String instanceId;
	private String instanceZone;
	public Instance runningInstance;
	private ArrayList<String> secGroup;
	private String keyName;
	
	public MyInstance (AmazonEC2 iec2, AWSCredentials credentials, String keyname, ArrayList<String> secGroup)
	{
		this.cre = credentials;
		this.ec2 = iec2;
		this.keyName=keyname;
		this.secGroup=secGroup;
	}
	
	public void setSecGroup(ArrayList<String> gn){
		this.secGroup = gn;
	}
	
	public void setKeyName(String kn){
		this.keyName = kn;
	}
	
	public String getInstanceId(){
		return instanceId;
	}
	
	public void createInstance() throws Exception{
		String imageId = "ami-16a3577f";
		int minInstanceCount = 1;
		int maxInstanceCount = 1;
		RunInstancesRequest rir = new RunInstancesRequest(imageId, minInstanceCount, maxInstanceCount);
        rir.setKeyName(keyName);
        rir.setSecurityGroupIds(secGroup);
		rir.setInstanceType("m1.small");
		rir.setPlacement(new Placement("us-east-1a"));
		
		RunInstancesResult result = ec2.runInstances(rir);
		runningInstance = result.getReservation().getInstances().get(0);
		
		
		List<Instance> resultInstance = result.getReservation().getInstances();
		
        for (Instance ins : resultInstance){
        	instanceId = ins.getInstanceId();
        	
        	System.out.println("New instance has been created: "+ins.getInstanceId());
        }
        
        String state = "";
        DescribeInstancesRequest direq = new DescribeInstancesRequest();
        List<String> resous = new LinkedList<String>();
        resous.add(instanceId);
        direq.setInstanceIds(resous);
        Instance in = null;
        Thread.sleep(10000);
        while(state.equals("running")==false){
        	DescribeInstancesResult dires = ec2.describeInstances(direq);
        	List<Reservation> resers = dires.getReservations();
        	Reservation reser = (Reservation) resers.get(0);
        	in = reser.getInstances().get(0);
        	InstanceState ins = in.getState();
        	state = ins.getName();
        	System.out.println("The instance is: " + state);
        	Thread.sleep(10000);
        }
        System.out.println("The instance is: " + state);
        System.out.println(in.toString());
        
        
        
        List<String> resources = new LinkedList<String>();
        List<Tag> tags = new LinkedList<Tag>();
        Tag nameTag = new Tag("Name", "CloudComputing");
        
        resources.add(instanceId);
        tags.add(nameTag);
        
        CreateTagsRequest ctr = new CreateTagsRequest(resources, tags);
        ec2.createTags(ctr);
	}
	
	public void startInstance(){
		List<String> instanceIds = new LinkedList<String>();
        instanceIds.add(instanceId);
        StartInstancesRequest startIR = new StartInstancesRequest(instanceIds);
        ec2.startInstances(startIR);
	}
	
	public void stopInstance(){
		List<String> instanceIds = new LinkedList<String>();
        instanceIds.add(instanceId);
        StopInstancesRequest stopIR = new StopInstancesRequest(instanceIds);
        ec2.stopInstances(stopIR);
	}
	
	public void terminateInstance(){
		List<String> instanceIds = new LinkedList<String>();
        instanceIds.add(instanceId);
		TerminateInstancesRequest tir = new TerminateInstancesRequest(instanceIds);
		tir.setInstanceIds(instanceIds);
		ec2.terminateInstances(tir);
	}
	
	
}
